package com.wyzz.hr.util;

import com.opensymphony.xwork2.XWorkException;
import java.text.*;
import java.util.*;
import ognl.DefaultTypeConverter;

public class DateConverter extends DefaultTypeConverter {

	public DateConverter() {
	}

	public Object convertValue(Map ognlContext, Object value, Class toType) {
		Object result = null;
		if (toType == java.util.Date.class)
			result = doConvertToDate(value);
		else if (toType == java.lang.String.class)
			result = doConvertToString(value);
		return result;
	}

	private Date doConvertToDate(Object value) {
		Date result = null;
		if (value instanceof String) {
			Locale locale = Locale.getDefault();
			SimpleDateFormat simpDate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat datchDate = new SimpleDateFormat("yyyy/MM/dd");
			SimpleDateFormat d1 = (SimpleDateFormat) DateFormat
					.getDateTimeInstance(3, 1, locale);
			SimpleDateFormat d2 = (SimpleDateFormat) DateFormat
					.getDateTimeInstance(3, 2, locale);
			SimpleDateFormat d3 = (SimpleDateFormat) DateFormat
					.getDateTimeInstance(3, 3, locale);
			SimpleDateFormat dfs[] = { simpDate, sdf, datchDate, d1, d2, d3 };
			for (int i = 0; i < dfs.length; i++)
				try {
					result = dfs[i].parse((String) value);
					if (result != null)
						break;
				} catch (ParseException ignore) {
				}

			if (result == null)
				throw new XWorkException(
						"Could not parse date- com.hr.util.DateConverter.java 48 line...");
		} else if (value instanceof Object[]) {
			Object array[] = (Object[]) (Object[]) value;
			if (array != null && array.length >= 1) {
				value = array[0];
				result = doConvertToDate(value);
			}
		} else if (java.util.Date.class.isAssignableFrom(value.getClass()))
			result = (Date) value;
		return result;
	}

	private String doConvertToString(Object value) {
		String result = null;
		if (value instanceof Date)
			result = sdf.format(value);
		return result;
	}

	private static final SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd");

}
